package com.example.friendcash.custom.views;

import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

public class SecureImageView extends ImageView {

    private static final String TAG = SecureImageView.class.getName();

    private boolean measured = false;

    public SecureImageView(Context context) {
        super(context);
    }

    public SecureImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measured = true;
    }

    public void secureSetImage(final String asset) {
        recycleCurrentImage();
        if (measured)
            try {
                secureSetImageInternal(asset);
            } catch (IOException e) {
                e.printStackTrace();
            }
        else postDelayed(new Runnable() {
            @Override
            public void run() {
                secureSetImage(asset);
            }
        }, 100);
    }

    private void recycleCurrentImage() {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) getDrawable();
        if (bitmapDrawable != null)
            bitmapDrawable.getBitmap().recycle();
    }

    private void secureSetImageInternal(String asset) throws IOException {
        Log.d(TAG, String.format("view size: %dx%d", getWidth(), getHeight()));
        // read raw image dimensions
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(
                getContext().getAssets().open(asset), null, options);
        int imageWidth = options.outWidth;
        int imageHeight = options.outHeight;
        Log.d(TAG, String.format("raw image: %dx%d", imageWidth, imageHeight));
        // get scaled image
        float scaleX = (float) imageWidth / getWidth();
        float scaleY = (float) imageHeight / getHeight();
        int sampleSize = Math.round(Math.min(scaleX, scaleY));
        secureSetImageInternalTwice(asset, sampleSize);
    }

    private void secureSetImageInternalTwice(String asset, int sampleSize)
            throws IOException {
        Log.d(TAG, "sample size: " + sampleSize);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inSampleSize = sampleSize;
        try {
            Bitmap decoded = BitmapFactory.decodeStream(
                    getContext().getAssets().open(asset), null, options);
            if (decoded == null) return; // we can't read that image
            int imageWidth = decoded.getWidth();
            int imageHeight = decoded.getHeight();
            Log.d(TAG, String.format("scaled image: %dx%d", imageWidth, imageHeight));
            setImageBitmap(decoded);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            Log.d(TAG, "OOM, downsampling...");
            secureSetImageInternalTwice(asset, sampleSize + 1);
        }
    }

}
